<template>
  <div>
    <AuthComponent></AuthComponent>
    <div>
      <el-button type="primary" @click="goMyBlog()" style="margin: 0 60px 20px">返回我的博客</el-button>
    </div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="标题" prop="cgName">
        <el-input v-model="ruleForm.cgName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save('ruleForm')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import AuthComponent from "@/components/commen/AuthComponent";

export default {
  name: "CategorySave",
  components: {
    AuthComponent
  },
  data() {
    return {
      userInfo: {},
      ruleForm: {
        cgName: '',
      },
      rules: {
        cgName: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.userInfo = this.$store.state.userInfo
    // 获取到路径上cgId
    let cgId = this.$route.params.cgId
    if (!cgId) {
      return;
    }
    if (cgId == 'undefined') {
      return;
    }
    // 查询分类内容
    this.$http.get("/category/detail/" + cgId).then(res => {
      res = res.data
      if (res.code === 0) {
        this.ruleForm = res.data;
      }
    }).catch(err => {
      console.log(err)
    })
  },
  methods: {
    goMyBlog() {
      this.$router.push({path: '/blog/person/list'})
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http.post("/category/save", this.ruleForm, {headers: {token: this.userInfo.token}}).then(res => {
            res = res.data;
            if (res.code === 0) {
              this.$message({
                message: '保存成功',
                type: 'success'
              });
              this.$router.push({ path: `/category/list/`})
            } else {
              this.$message({
                message: '保存失败',
                type: 'warning'
              });
            }
          }).catch((error) => {
            console.log(error);
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>

</style>